package com.example.clock.lockedPage;

import static com.example.clock.controlClass.SqlController.addPlan;
import static com.example.clock.controlClass.SqlController.getAllPlans;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.clock.R;
import com.example.clock.basicClass.Plan;
import com.example.clock.basicClass.Task;
import com.example.clock.controlClass.DatabaseHelper;

import java.io.Serializable;
import java.util.ArrayList;

public class PlanLockScreen extends AppCompatActivity {

    private Spinner spinner;
    private String planID;
    private String planName;
    private String largeTag;//大标签
    private String smallTag;//小标签
    private int lastTimeByTime;

    ArrayList<Plan> planArrayList = new ArrayList<Plan>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_plan_lock_screen);

        //Toast.makeText(getApplicationContext(), "计划锁屏。", Toast.LENGTH_LONG).show();

        /* for test only , add some plans
        DatabaseHelper dbhelper1 = new DatabaseHelper(PlanLockScreen.this,PlanLockScreen.this);
        SQLiteDatabase db1 = dbhelper1.getReadableDatabase();
        Plan plan1 = new Plan("plan1");
        plan1.setPlanId("p1");
        plan1.setPlanName("plan1");
        plan1.setLargeTags("学习");
        plan1.setSmallTags("英语");
        plan1.setLastTimeByTimes(1);
        addPlan(db1,plan1);
        Plan plan2 = new Plan("plan2");
        plan2.setPlanId("p2");
        plan2.setPlanName("plan2");
        plan2.setLargeTags("学习");
        plan2.setSmallTags("地理");
        plan2.setLastTimeByTimes(2);
        addPlan(db1,plan2);
        Plan plan3 = new Plan("plan3");
        plan3.setPlanId("p3");
        plan3.setPlanName("plan3");
        plan3.setLargeTags("休息");
        plan3.setSmallTags("睡觉");
        plan3.setLastTimeByTimes(3);
        addPlan(db1,plan3);*/

        //获取所有plan
        DatabaseHelper dbhelper = new DatabaseHelper(PlanLockScreen.this,PlanLockScreen.this);
        SQLiteDatabase db = dbhelper.getReadableDatabase();
        planArrayList =getAllPlans(db);

        String planNameList[] = new String[planArrayList.size()];
        for (int i=0;i<planArrayList.size();i++){
            planNameList[i] = planArrayList.get(i).getPlanName();
        }
        //设置破烂下拉框列表
        spinner = (Spinner) findViewById(R.id.spinner5);
        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, planNameList);
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter1);


        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {
            //根据选择的plan，显示相应的锁屏标签
            @Override
            public void onItemSelected(AdapterView<?> parent,View view, int position, long id)
            {
                System.out.println("plan position =  "+position);

                planID = planArrayList.get(position).getPlanId();
                planName = planArrayList.get(position).getPlanName();
                largeTag = planArrayList.get(position).getLargeTags();
                smallTag = planArrayList.get(position).getSmallTags();
                lastTimeByTime = planArrayList.get(position).getLastTimeByTimes();

                TextView pc = (TextView) findViewById(R.id.textView13);
                pc.setText(largeTag);//一级事件
                TextView sc = (TextView) findViewById(R.id.textView14);
                sc.setText(smallTag);//二级事件
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }

        });

    }

    public  void onButtonCancle(View view){
        finish();
    }

    public void onButtonConfirm(View view){
        Toast.makeText(getApplicationContext(), "计划锁屏确认。", Toast.LENGTH_LONG).show();
        Task task = new Task();

        //组装task，并传给锁屏界面
        task.setPlanId(planID);//绑定planID；
        task.setTaskName(planName);//用planName代替taskName；
        task.setPlanTime(lastTimeByTime);//锁屏时间为plan.lastTimeByTimes
        task.setLargeTags(largeTag);
        task.setSmallTags(smallTag);
        task.setClockedMode(1);//默认简单模式
        task.setTips("");

        Intent intent = new Intent(PlanLockScreen.this, com.example.clock.lockedPage.LockActivity.class);
        intent.putExtra("Serializable", (Serializable) task);//设置Serializable传递数据
        startActivity(intent);//开启锁屏界面

    }

}